class Solution {
public:
    vector<int> canSeePersonsCount(vector<int>& heights) {
        stack<int> s;
        vector<int> ans(heights.size());
        for (int i = heights.size() - 1; i >= 0; --i) {
            while (s.size() && s.top() < heights[i]) {
                ans[i]++;
                s.pop();
            }
            ans[i] += !s.empty();
            s.push(heights[i]);
        }
        return ans;
    }
};